package net.butfly.bus.util;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;

import net.butfly.albacore.utils.UtilsBase;
import net.butfly.bus.TX;

public class TXUtils extends UtilsBase {
	public static final TX TXImpl(final String tx) {
		return TXImpl(tx, TX.INIT_VERSION);
	}

	public static final TX TXImpl(final TX tx) {
		return TXImpl(tx.value(), tx.version());
	}

	public static TX TXImpl(final String tx, final String version) {
		String key = tx + ":" + version;
		TX impl = TX_IMPL_POOL.get(key);
		if (null == impl) {
			impl = new TX() {
				@Override
				public Class<? extends Annotation> annotationType() {
					return TX.class;
				}

				@Override
				public String value() {
					return tx;
				}

				@Override
				public String version() {
					return version;
				}
			};
			TX_IMPL_POOL.put(key, impl);
		}
		return impl;
	}

	private static final Map<String, TX> TX_IMPL_POOL = new HashMap<String, TX>();

	public static final String key(TX tx) {
		return key(tx.value(), tx.version());
	}

	public static final String key(String code, String version) {
		return code + ":" + version;
	}
}
